package com.rboat.web.controller.blog;

import com.rboat.blog.domain.BlogFriend;
import com.rboat.blog.service.IBlogFriendService;
import com.rboat.common.annotation.Log;
import com.rboat.common.core.controller.BaseController;
import com.rboat.common.core.domain.AjaxResult;
import com.rboat.common.core.page.TableDataInfo;
import com.rboat.common.enums.BusinessType;
import com.rboat.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 好友Controller
 * 
 * @author rboat
 * @date 2019-11-05
 */
@Controller
@RequestMapping("/blog/friend")
public class BlogFriendController extends BaseController
{
    private String prefix = "blog/friend";

    @Autowired
    private IBlogFriendService blogFriendService;

    @RequiresPermissions("blog:friend:view")
    @GetMapping()
    public String friend()
    {
        return prefix + "/friend";
    }

    /**
     * 查询好友列表
     */
    @RequiresPermissions("blog:friend:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BlogFriend blogFriend)
    {
        startPage();
        List<BlogFriend> list = blogFriendService.selectBlogFriendList(blogFriend);
        return getDataTable(list);
    }

    /**
     * 导出好友列表
     */
    @RequiresPermissions("blog:friend:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BlogFriend blogFriend)
    {
        List<BlogFriend> list = blogFriendService.selectBlogFriendList(blogFriend);
        ExcelUtil<BlogFriend> util = new ExcelUtil<BlogFriend>(BlogFriend.class);
        return util.exportExcel(list, "friend");
    }

    /**
     * 新增好友
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存好友
     */
    @RequiresPermissions("blog:friend:add")
    @Log(title = "好友", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BlogFriend blogFriend)
    {
        return toAjax(blogFriendService.insertBlogFriend(blogFriend));
    }

    /**
     * 修改好友
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        BlogFriend blogFriend = blogFriendService.selectBlogFriendById(id);
        mmap.put("blogFriend", blogFriend);
        return prefix + "/edit";
    }

    /**
     * 修改保存好友
     */
    @RequiresPermissions("blog:friend:edit")
    @Log(title = "好友", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BlogFriend blogFriend)
    {
        return toAjax(blogFriendService.updateBlogFriend(blogFriend));
    }

    /**
     * 删除好友
     */
    @RequiresPermissions("blog:friend:remove")
    @Log(title = "好友", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(blogFriendService.deleteBlogFriendByIds(ids));
    }
}
